#include "rtc_app.h"
#include "rtc_board.h"

#if (_REAL_TIME_CLOCK_SOURCE_ == _INTERNAL_CLOCK_SOURCE_)
static uint32_t RtcTimeCompensation = 0;

void RtcGetDeviceTime(TimeFormat_t *pDeviceTime)
{
  if (pDeviceTime != NULL)
  {
    TimeFormat_t DeviceTime = {0};
    RTC_GetTime(&DeviceTime.Year, &DeviceTime.Month, &DeviceTime.Date, &DeviceTime.Hour, &DeviceTime.Minute, &DeviceTime.Second);
    uint32_t DeviceTimestamp = MakeTimestamp(&DeviceTime);

    uint32_t ConversionsTimestamp = DeviceTimestamp + RtcTimeCompensation;
    TimestampToNormalTime(ConversionsTimestamp, pDeviceTime);
  }
}

bool RtcUpdateDeviceTime(uint32_t UpdateTimestamp)
{
  TimeFormat_t DeviceTime = {0};

  RTC_GetTime(&DeviceTime.Year, &DeviceTime.Month, &DeviceTime.Date, &DeviceTime.Hour, &DeviceTime.Minute, &DeviceTime.Second);
  uint32_t DeviceTimestamp = MakeTimestamp(&DeviceTime);

  if (UpdateTimestamp > DeviceTimestamp)
  {
    RtcTimeCompensation = UpdateTimestamp - DeviceTimestamp;
    return true;
  }

  return false;
}

#endif

